package com.xyh.builder;

import com.xyh.bean.Constants;
import com.xyh.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * 构建应用配置文件
 */
public class BuildAppProperties {
    private static final Logger logger = LoggerFactory.getLogger(BuildAppProperties.class);

    public static void execute() {

        // 初始化目录和文件
        File poDir = new File(Constants.PATH_RESOURCES);
        if (!poDir.exists() && !poDir.mkdirs()) {
            logger.error("创建Resource目录失败: {}", Constants.PATH_RESOURCES);
            return;
        }
        File propFile = new File(Constants.PATH_RESOURCES, "application.properties");

        try (BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(propFile), StandardCharsets.UTF_8))) {

            // 写入应用基础配置
            writeAppBaseConfig(bw);

            // 写入数据源配置
            writeDataSourceConfig(bw);

            // 写入MyBatis-Plus配置
            writeMybatisPlusConfig(bw);

            // 写入Redis配置
            writeRedisConfig(bw);

            bw.flush();
            logger.info("生成application.properties成功: {}", propFile.getAbsolutePath());
        } catch (Exception e) {
            logger.error("生成application.properties失败", e);
        }
    }

    private static void writeAppBaseConfig(BufferedWriter bw) throws IOException {
        bw.write("#====================== 应用基础配置 ======================\n");
        bw.write("# 服务器启动端口（生产环境建议使用8080以上端口）\n");
        bw.write("server.port="+Constants.SERVER_PORT+"\n");
//        bw.write("spring.application.name=EasyJavaCodeGenerator\n");
//        bw.write("spring.profiles.active=dev\n\n");
    }

    private static void writeDataSourceConfig(BufferedWriter bw) throws IOException {
        bw.write("#====================== 数据源配置 ======================\n");
        bw.write("# 数据库连接配置\n");
        bw.write("spring.datasource.driver-class-name="+Constants.DB_DRIVER_NAME+"\n");
        bw.write("spring.datasource.url="+Constants.DB_URL+"\n");
        bw.write("spring.datasource.username="+Constants.DB_USERNAME+"\n");
        bw.write("spring.datasource.password="+Constants.DB_PASSWORD+"\n");
        bw.write("spring.datasource.hikari.maximum-pool-size=20\n");
        bw.write("spring.datasource.hikari.minimum-idle=5\n");
        bw.write("spring.datasource.hikari.idle-timeout=30000\n");
        bw.write("spring.datasource.hikari.connection-timeout=30000\n");
        bw.write("spring.datasource.hikari.max-lifetime=1800000\n\n");
    }

    private static void writeMybatisPlusConfig(BufferedWriter bw) throws IOException {
        bw.write("#====================== MyBatis-Plus 配置 ======================\n");
        bw.write("# 实体类所在包（自动注册类型别名）\n");
        bw.write("mybatis-plus.type-aliases-package=" + Constants.PACKAGE_PO + "\n");
        bw.write("# Mapper XML 文件位置配置\n");
        bw.write("mybatis-plus.mapper-locations=classpath*:/mapper/**/*.xml\n");
        bw.write("# 全局配置（ID生成策略设置为数据库自增）\n");
        bw.write("mybatis-plus.global-config.db-config.id-type=auto\n");
        bw.write("# MyBatis 原生配置\n");
        bw.write("mybatis-plus.configuration.map-underscore-to-camel-case=true\n");
        bw.write("mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl \n\n");
    }

    private static void writeRedisConfig(BufferedWriter bw) throws IOException {
        bw.write("#====================== Redis 配置 ======================\n");
        bw.write("# Redis连接配置（生产环境建议使用云Redis服务）\n");
        bw.write("spring.data.redis.host="+Constants.REDIS_HOST+"\n");
        bw.write("spring.data.redis.port="+Constants.REDIS_PORT+"\n");
        bw.write("spring.data.redis.database="+Constants.REDIS_DB+"\n");
        bw.write("# 可选配置（根据需要添加）\n");

        bw.write("# spring.data.redis.password=${REDIS_PASSWORD}\n");
        bw.write("# spring.data.redis.timeout=3000\n");
        bw.write("# spring.data.redis.ssl=false\n\n");
    }
}